package com.example.imtoken.modules.main.wallet.manage

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.LogUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema

class ManageViewModel : BaseViewModel() {

    val isDeleteOk = MutableLiveData(false)

    private val defAccountLiveData = MutableLiveData<AccountSchema>()

    fun getDefAccountData(): MutableLiveData<AccountSchema> {
        return defAccountLiveData
    }

    fun getDefAccount() {
        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLogin.walletAddress)
        defAccountLiveData.value = account
    }

    fun getDefAccount(key: String?) {
        val account = AppDataBase.instance.getAccountDao().getDefAccount(key)
        defAccountLiveData.value = account
    }

    fun updateWalletName(walletName: String) {
//        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
//        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLogin.walletAddress)
//        account.name = walletName
        val account = defAccountLiveData.value ?: return
        account.name = walletName
        if (AppDataBase.instance.getAccountDao().updateAccount(account) > 0) {
            defAccountLiveData.value = account
            BusUtils.postSticky(BusConfig.update_default_account_sticky_wallet_fragment)
        }
    }

    fun deleteWallet() {
        val data = defAccountLiveData.value
        data?.let {
            if (AppDataBase.instance.getAccountDao().deleteAccount(it.address) > 0) {
                val deleteCount =
                    AppDataBase.instance.getWalletCheckedDao().deleteAddress(it.address)
                if (deleteCount > 0) LogUtils.e("删除了-- wallet_checked表 --${deleteCount}条数据")

                val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
                val account = AppDataBase.instance.getAccountDao().findOwnerData()
                appLogin.walletAddress = account.address
                if (AppDataBase.instance.getAppLoginDao().updateAppLogin(appLogin) > 0) {
                    LogUtils.e("更新默认钱包成功")
                    BusUtils.postSticky(BusConfig.update_default_account_sticky_wallet_fragment)
                    isDeleteOk.value = true
                } else {
                    LogUtils.e("更新默认钱包失败")
                    mErrorResId.postValue(R.string.replaceWordError)
                }
            } else {
                mErrorResId.postValue(R.string.deleteWordError)
            }
        }
    }

    fun walletPass(pass: String): Boolean {
        val data = defAccountLiveData.value
        data?.let {
            return if (AppDataBase.instance.getAccountDao().findWalletPass(it.address, pass) > 0) {
                true
            } else {
                mErrorResId.postValue(R.string.passWordError)
                false
            }
        }
        return false
    }

}